package com.ezbuy.common.helper;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class ClassUtil {

    /**
     * 获取到当前类或接口的所有子类
     *
     * @param c
     * @return
     * @throws ClassNotFoundException
     * @throws IOException
     */
    public static List<Class<?>> getAllClasses(Class<?> c) throws ClassNotFoundException, IOException {
        List<Class<?>> cls = new ArrayList<Class<?>>();
        String packageName = c.getPackage().getName();
        List<Class<?>> allClass = getClasses(packageName);
        for (int i = 0; i < allClass.size(); i++) {
            if (c.isAssignableFrom(allClass.get(i))) {
                if (!c.equals(allClass.get(i))) {
                    cls.add(allClass.get(i));
                }
            }
        }
        return cls;
    }

    /**
     * 根据包名查询出所有子类信息
     *
     * @param packageName
     * @return
     * @throws ClassNotFoundException
     * @throws IOException
     */
    public static List<Class<?>> getClasses(String packageName) throws ClassNotFoundException, IOException {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        String path = packageName.replace(".", "/");
        Enumeration<URL> resources = classLoader.getResources(path);
        List<File> dirs = new ArrayList<File>();
        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            dirs.add(new File(resource.getFile()));
        }
        List<Class<?>> classes = new ArrayList<Class<?>>();
        for (File directory : dirs) {
            classes.addAll(findClass(directory, packageName));
        }
        return classes;
    }

    /**
     * 获取指定目录&包路径下的所有类
     *
     * @param directory   文件目录路径,包含包名路径
     * @param packageName 包名路径
     * @return
     * @throws ClassNotFoundException
     */
    public static List<Class<?>> findClass(File directory, String packageName)
            throws ClassNotFoundException {
        List<Class<?>> classes = new ArrayList<Class<?>>();
        if (!directory.exists()) {
            return classes;
        }
        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                assert !file.getName().contains(".");
                classes.addAll(findClass(file, packageName + "." + file.getName()));
            } else if (file.getName().endsWith(".class")) {
                String className = file.getName().substring(0, file.getName().length() - 6);
                classes.add(Class.forName(packageName + "." + className));
            }
        }
        return classes;
    }

    /**
     * 获取指定目录&包路径下的所有类
     *
     * @param root        文件目录根路径,不包含包名路径
     * @param packageName 包名路径
     * @return
     * @throws ClassNotFoundException
     */
    public static List<Class<?>> findClassInRootDir(File root, String packageName)
            throws ClassNotFoundException {
        String path = packageName.replace(".", "/");
        File directory=new File(root+File.separator+path);
        return findClass(directory,packageName);
    }

}